package com.gcd.api;

import com.gcd.model.Member;
import com.gcd.service.RegisterService;
import com.gcd.utils.JsonResult;
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/v1/register")
public class RegisterApi {
    private final static StrongPasswordEncryptor PE=new StrongPasswordEncryptor();
    private RegisterService registerService;
    @Autowired

    public void setRegisterService(RegisterService registerService) {
        this.registerService = registerService;
    }
    @PutMapping
    public ResponseEntity<JsonResult<?>> register(@RequestBody Member member) {
        if (registerService.isPhoneExists(member.getPhone())) {
            return ResponseEntity.ok(JsonResult.fail("该手机号已被注册"));
        }
        String password = member.getPassword();
        String strongPassword=PE.encryptPassword(password);
        member.setPassword(strongPassword);
        member.setVip(1);
        member.setCreateTime(LocalDateTime.now());
        boolean register = registerService.register(member);
        if (register) {
            return ResponseEntity.ok(JsonResult.success(null, "注册成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("注册失败"));
        }
    }
}
